﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
namespace DAO
{
    public class AdministratorDAO
    {
        public static DataClasses1DataContext Data = new DataClasses1DataContext();
        public static List<MEMBER> GetAllAdministrator()
        {
         
            var query = from admin in Data.MEMBERs where ((admin.ACCOUNT.VISIBLE==true)&&(admin.MEMBER_STYLE==1)) select admin;
            List<MEMBER> ListAdmin = new List<MEMBER>();
            foreach (MEMBER admin in query)
                ListAdmin.Add(admin);
            return ListAdmin;
        }

        public static MEMBER GetAdministrator(int IDAdmin)
        {          
            return Data.MEMBERs.Single(Admin => Admin.IDMEMBER == IDAdmin);
        }

        public static MEMBER GetAdministrator(string UserName, string PassWord)
        {
           
            return Data.MEMBERs.Single(Admin => (Admin.ACCOUNT.USERNAME.Trim() == UserName)&& (Admin.ACCOUNT.PASSWORD.Trim()==PassWord));
        }

        public static bool Insert(MEMBER Admin)
        {
            try
            {             
                Data.MEMBERs.InsertOnSubmit(Admin);
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        
        
        public static bool IsExisting(int IDAdmin)
        {
            return Data.MEMBERs.Any(admin => ((admin.IDMEMBER == IDAdmin)&&(admin.ACCOUNT.VISIBLE==true)&&(admin.MEMBER_STYLE==1)));
        }


        public static bool IsExisting(string UserName)
        {
            return Data.MEMBERs.Any(Admin =>((Admin.ACCOUNT.USERNAME.Trim() == UserName)&&(Admin.ACCOUNT.VISIBLE==true)&&(Admin.MEMBER_STYLE==1)));
        }

        public static bool IsExisting(string UserName,string PassWord)
        {
            return Data.MEMBERs.Any(Admin => ((Admin.ACCOUNT.USERNAME.Trim() == UserName) && (Admin.ACCOUNT.PASSWORD.Trim() == PassWord) && (Admin.ACCOUNT.VISIBLE == true) && (Admin.MEMBER_STYLE == 1)));
        }

        public static bool updateMember(MEMBER mMember)
        {
            try
            {
                MEMBER member = Data.MEMBERs.Single(temp => temp.ACCOUNT.USERNAME == mMember.ACCOUNT.USERNAME);
                member.ACCOUNT.ADDRESS = mMember.ACCOUNT.ADDRESS;
                member.ACCOUNT.CMND = mMember.ACCOUNT.CMND;
                member.ACCOUNT.FULLNAME = mMember.ACCOUNT.FULLNAME;
                member.ACCOUNT.PASSWORD = mMember.ACCOUNT.PASSWORD;
                member.ACCOUNT.PHONE_NUMBER = mMember.ACCOUNT.PHONE_NUMBER;
                Data.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
